from models import BasicTextStyle


def convert_to_subtitle_clip(related_info, video_time):
    """
     处理额外字幕的方法
    """

    subtitle_clips = []
    for info in related_info:
        basic_text_style = BasicTextStyle.query.filter_by(id=info.style_id).first()
        if info.type_in == 1:
            if float(info.start_time) > video_time:
                subtitle_clip = {
                    "SubtitleTrackClips": [{}]
                }
                subtitle_clips.append(subtitle_clip)
                continue  # 使用continue而不是return，继续循环
            else:
                TimelineIn = float(info.start_time)
            # 结束前
        if info.type_in == 2:
            if float(info.end_time) < video_time:
                time = video_time - float(info.end_time)
                TimelineOut = time
            else:
                subtitle_clip = {
                    "SubtitleTrackClips": [{}]
                }
                subtitle_clips.append(subtitle_clip)
                continue  # 使用continue而不是return，继续循环
            # 开始后
        if info.type_out == 1:
            if float(info.start_time) > video_time:
                subtitle_clip = {
                    "SubtitleTrackClips": [{}]
                }
                subtitle_clips.append(subtitle_clip)
                continue  # 使用continue而不是return，继续循环
            else:
                TimelineIn = float(info.start_time)
            # 结束前
        if info.type_out == 2:
            if float(info.end_time) < video_time:
                time = video_time - float(info.end_time)
                TimelineOut = time
            else:
                subtitle_clip = {
                    "SubtitleTrackClips": [{}]
                }
                subtitle_clips.append(subtitle_clip)
                continue  # 使用continue而不是return，继续循环
        subtitle_clip = {
            "SubtitleTrackClips": [{
                "TimelineIn": TimelineIn,
                "TimelineOut": TimelineOut,
                "X":info.x,
                'Y':info.y,
                "Type": "Text",
                "Content": info.text,
                "Font": basic_text_style.font,
                "FontSize": info.size,
                "FontColor": basic_text_style.font_color,
                "FontFace": {
                    "Bold": basic_text_style.bold,
                    "Italic": basic_text_style.italic,
                    "Underline": basic_text_style.underline,
                },
                "Alignment": info.align,
                "Shadow": basic_text_style.shadow_size,
                "BackColour": basic_text_style.shadow_color,
                "Outline": basic_text_style.outline,
                "OutlineColour": basic_text_style.outline_colour,
                "BorderStyle": basic_text_style.border_style
            }

        ]}

        subtitle_clips.append(subtitle_clip)

    return subtitle_clips
